package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exceptions.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.model.enums.ReleaseStatus;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentQueryVo;
import com.atguigu.lease.web.admin.vo.apartment.ApartmentSubmitVo;
import com.atguigu.lease.web.admin.vo.fee.FeeValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {
    @Autowired
    private ApartmentLabelService apartmentLabelService;
    @Autowired
    private ApartmentFacilityService apartmentFacilityService;
    @Autowired
    private ApartmentFeeValueService apartmentFeeValueService;
    @Autowired
    private GraphInfoService graphInfoService;

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;

    @Autowired
    private GraphInfoMapper graphInfoMapper;

    @Autowired
    private LabelInfoMapper labelInfoMapper;


    @Autowired
    private FacilityInfoMapper facilityInfoMapper;

    @Autowired
    private FeeValueMapper feeValueMapper;

    @Autowired
    private RoomInfoService roomInfoService;


    /**
     * 新增公寓功能关系到5张表，多表操作，要自定义方法
     * @param apartmentSubmitVo
     */
    @Override
    public void saveOrUpdateAll(ApartmentSubmitVo apartmentSubmitVo) {

        //根据id是否为空判断是修改还是保存，目前有问题
        boolean isUpate=apartmentSubmitVo.getId()!=null;
        //apartmentSubmitVo继承自ApartmentInfo(公寓实体类)，所以可以当saveOrUpdate的参数
        saveOrUpdate(apartmentSubmitVo);//对于公寓表的操作完成。
        Long apartmentId=apartmentSubmitVo.getId();
        if(isUpate){
            //修改要先删除其他表和该公寓关联信息
            //删除公寓标签表的信息
            LambdaQueryWrapper<ApartmentLabel> labelInfoLambdaQueryWrapper=new LambdaQueryWrapper<>();
            labelInfoLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId,apartmentId);
            apartmentLabelService.remove(labelInfoLambdaQueryWrapper);

            //删除公寓&配套关联表信息
            LambdaQueryWrapper<ApartmentFacility> facilityLambdaQueryWrapper=new LambdaQueryWrapper<>();
            facilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId,apartmentId);
            apartmentFacilityService.remove(facilityLambdaQueryWrapper);

            //删除公寓&杂费关联表信息
            LambdaQueryWrapper<ApartmentFeeValue> feeValueLambdaQueryWrapper=new LambdaQueryWrapper<>();
            feeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId,apartmentId);
            apartmentFeeValueService.remove(feeValueLambdaQueryWrapper);

            //删除图片信息表和公寓的相关信息
            LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper=new LambdaQueryWrapper<>();
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
            //因为ItemId可能是公寓可能是房间，公寓id和房间id会重复,所以类型和Id都要判断
            graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId,apartmentId);
            graphInfoService.remove(graphInfoLambdaQueryWrapper);

        }
        //把传过来的其他表的信息保存到数据库

        //保存公寓标签表的信息
        if (!CollectionUtils.isEmpty(apartmentSubmitVo.getLabelIds())) {
            List<ApartmentLabel> apartmentLabelList = new ArrayList<>();
            List<Long> labelIds = apartmentSubmitVo.getLabelIds();
            for (Long labelId : labelIds) {
                apartmentLabelList.add(ApartmentLabel.builder().apartmentId(apartmentId).labelId(labelId).build());
            }
            apartmentLabelService.saveBatch(apartmentLabelList);
        }

        //保存公寓&配套关联表信息
        if (!CollectionUtils.isEmpty(apartmentSubmitVo.getFacilityInfoIds())) {
            List<ApartmentFacility> facilityList = new ArrayList<>();
            List<Long> facilityInfoIds = apartmentSubmitVo.getFacilityInfoIds();
            for (Long facilityInfoId : facilityInfoIds) {
                facilityList.add(ApartmentFacility.builder().apartmentId(apartmentId).facilityId(facilityInfoId).build());
            }
            apartmentFacilityService.saveBatch(facilityList);
        }

        //保存公寓&杂费关联表信息
        if (!CollectionUtils.isEmpty(apartmentSubmitVo.getFeeValueIds())) {
            List<ApartmentFeeValue> feeValueList = new ArrayList<>();
            List<Long> feeValueIds = apartmentSubmitVo.getFeeValueIds();
            for (Long feeValueId : feeValueIds) {
                feeValueList.add(ApartmentFeeValue.builder().apartmentId(apartmentId).feeValueId(feeValueId).build());
            }
            apartmentFeeValueService.saveBatch(feeValueList);
        }

        //保存图片信息表和公寓的相关信息
        if (!CollectionUtils.isEmpty(apartmentSubmitVo.getGraphVoList())) {
            List<GraphInfo> graphInfoList = new ArrayList<>();
            List<GraphVo> graphVoList = apartmentSubmitVo.getGraphVoList();
            for (GraphVo graphVo : graphVoList) {
                graphInfoList.add(GraphInfo.builder().url(graphVo.getUrl())
                        .name(graphVo.getName()).itemId(apartmentId)
                        .itemType(ItemType.APARTMENT).build());
            }
            graphInfoService.saveBatch(graphInfoList);
        }
    }

    /**
     * 根据条件分页查询公寓信息。多表查询
     * @param page
     * @param queryVo
     */
    @Override
    public Page<ApartmentItemVo> customPage(IPage<ApartmentItemVo> page, ApartmentQueryVo queryVo) {
        return apartmentInfoMapper.queryPage(page,queryVo);
    }


    /**
     * 根据ID获取公寓详细信息
     *  1.根据公寓id查询公寓的详情
     *  2.根据公寓id查询公寓的图片vo集合
     *  3.根据公寓id查询公寓的配套集合
     *  4.根据公寓id查询公寓的标签集合
     *  5.根据公寓id查询公寓的杂费集合
     *  6.组合数据到ApartmentDetailVo返回即可
     * @param id
     * @return
     */
    @Override
    public ApartmentDetailVo customDetail(Long id) {
        ApartmentDetailVo apartmentDetailVo=new ApartmentDetailVo();
        //根据公寓id查询公寓的详情
        ApartmentInfo apartmentInfo=getById(id);
        //根据公寓id查询公寓的图片vo集合
        List<GraphVo> graphVoList=graphInfoMapper.queryVoList(ItemType.APARTMENT,id);
        //return apartmentDetailVo;
        //根据公寓id查询公寓的配套集合
        List<LabelInfo> labelInfoList=labelInfoMapper.queryList(id);
        //根据公寓id查询公寓的标签集合
        List<FacilityInfo> facilityInfoList= facilityInfoMapper.queryList(id);
        //根据公寓id查询公寓的杂费集合
        List<FeeValueVo> feeValueVoList=feeValueMapper.queryVoList(id);
        //组合各个对象到ApartmentDetailVo
        BeanUtils.copyProperties(apartmentInfo,apartmentDetailVo);
        apartmentDetailVo.setGraphVoList(graphVoList);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        apartmentDetailVo.setFeeValueVoList(feeValueVoList);
        return apartmentDetailVo;
    }

    /**
     * 根据id删除公寓及其相关信息
     * 考虑到公寓关联到多个表结构！其中房间的数据较为重要！
     *  1.删除公寓之前，先考虑是否由房间，有不允许删除
     *  2.没有，删除公寓，以及公寓关联的四个中间表
     * @param id
     */
    @Override
    public void customRemove(Long id) {
        //公寓id , 发布状态的房间
        LambdaQueryWrapper<RoomInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(RoomInfo::getApartmentId,id);
        lambdaQueryWrapper.eq(RoomInfo::getIsRelease, ReleaseStatus.RELEASED);
        long count = roomInfoService.count(lambdaQueryWrapper);
        if (count >0){
            System.out.println("-------------删除错误");
            throw new LeaseException(ResultCodeEnum.DELETE_ERROR);
        }
        removeById(id);
        LambdaQueryWrapper<ApartmentLabel> labelInfoLambdaQueryWrapper=new LambdaQueryWrapper<>();
        labelInfoLambdaQueryWrapper.eq(ApartmentLabel::getApartmentId,id);
        apartmentLabelService.remove(labelInfoLambdaQueryWrapper);

        //删除公寓&配套关联表信息
        LambdaQueryWrapper<ApartmentFacility> facilityLambdaQueryWrapper=new LambdaQueryWrapper<>();
        facilityLambdaQueryWrapper.eq(ApartmentFacility::getApartmentId,id);
        apartmentFacilityService.remove(facilityLambdaQueryWrapper);

        //删除公寓&杂费关联表信息
        LambdaQueryWrapper<ApartmentFeeValue> feeValueLambdaQueryWrapper=new LambdaQueryWrapper<>();
        feeValueLambdaQueryWrapper.eq(ApartmentFeeValue::getApartmentId,id);
        apartmentFeeValueService.remove(feeValueLambdaQueryWrapper);

        //删除图片信息表和公寓的相关信息
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper=new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.APARTMENT);
        //因为ItemId可能是公寓可能是房间，公寓id和房间id会重复,所以类型和Id都要判断
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId,id);
        graphInfoService.remove(graphInfoLambdaQueryWrapper);

    }
}




